<?php
/** 
 * 结婚吧
 * Author:zhang
 *
 */

namespace Api\Controller;

class MarryController extends ApiController{
	public function __construct(){
    	parent::__construct();
    	/** 测试不判断登录 modify by zhang 2016 start  **/
    	if (!I('mytest')) {
	        !api_login() && response(-94);
	        !passed_login() && response(-95);
	        !only_login() && response(-96);
    	}
    	/** 测试不判断登录 modify by zhang 2016 end **/
	}

	//首页
	function index(){
		$sex = intval(I('sex',2));

		$list = M('marry')->alias('m')
				->field('m.name,m.id,m.birthday,m.litpic')
				->where("m.type=1 && m.sex=".$sex)
				->order('m.id desc')
				->select();
		foreach ($list as $key=>$val) {
            $list[$key]['age'] = time() - $list['birthday'];
            $list[$key]['age'] = floor($list['age']/31536000)+1;
            $cover_ids= explode(',', $val['litpic']);
            $list[$key]['imgPath'] = M('picture')->where('id='.$cover_ids[0])->getField('path');
        }
		response(1,$list);
	}
	function add(){
		$id = API_UID;
		$m_marry = M('marry');
		$data = $m_marry->create();
		$data['birthday'] = intval($data['birthday']);
        // 判断是添加还是修改
        $is_add = true;
        if ($id > 0) {
        	$check = $m_marry->where(array('id'=>$id))->count();
        	if ($check > 0) {
        		$is_add = false;
        	} else {
        	    $data['id'] = $id;
            }
        }

        if ($is_add) {
            $is_exist = $m_marry->where('id='.$id)->find();
            if ($is_exist) {
                response(-2, '', '该用户已存在');
            }
            $data['update_time']=NOW_TIME;
            $data['name'] = M('member')->where('uid='.$id)->getField('nickname');
        	$r = $m_marry->add($data);
        }else{
            $data['update_time']= NOW_TIME;
        	$r = $m_marry->where('id='.$id)->save($data);
        }

    	if ($r) {
    		response(1,"操作成功");
    	}else{
    		response(-9);
    	}
	}

    //删除个人资料
    public function delete() {
	    $id = API_UID;
	    $result = M('marry')->where('id='.$id)->delete();
	    $result?response(1):response(-9);
    }

	//个人资料
	function info(){
		params_exist(array('id'));
		$id = intval(I('id',0));
		$info = M('marry')->find($id);
		if(!$info){
            response(-9);
        }
        $cover_ids= explode(',', $info['litpic']);
        foreach ($cover_ids as $key1=>$value1) {
            $info['imgPath'][$key1] = M('picture')->where('id='.$value1)->getField('path');
        }
		$info['age'] = time() - $info['birthday'];
		$info['age'] = floor($info['age']/31536000)+1;
		response(1,$info);
	}
    //搜索
    function select(){
        //性别
        if(I('sex')){
        	$where['sex'] = I('sex');
        }
        //搜索词
        if(I('key')){
        	$where['name'] = array('like','%'.I('key').'%');
        }
        //地区
        if(I('region')){
        	$where['region'] = array('like','%'.I('region').'%');
        }
        //年龄
        if(I('age')){
            $age = explode(',',I('age'));
            $where['birthday'] = array('between',array(time()-$age[1]*31536000,time()-$age[0]*31536000));
        }
        //身高
        if(I('stature')){
            $stature = explode(',',I('stature'));
            $where['stature'] = array('between',array($stature[0],$stature[1]));
        }
        //学历
        if(I('education')){
        	$where['education'] = I('education');
        }
        //收入
        if(I('income')){
            $stature = explode(',',I('income'));
            $where['income'] = array('between',array($stature[0],$stature[1]));
        }
        //婚史
        if(I('married')){
        	$where['married'] = I('married');
        }
        //房
        if(I('room')){
        	$where['room'] = I('room');
        }
        //车
        if(I('car')){
        	$where['car'] = I('car');
        }
        $where['status'] = 1;
        $list = M('marry')->where($where)->select();
        response(1,$list);
    }
}